﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;
using DDDTest.Helper;

namespace DDDTest.Core {
    [TestClass]
    public class TestAttachable {
        [TestMethod]
        public void Test_New1 () {
            var nod = (Attachable)new Node ();

            Assert.AreEqual (0, nod.ExecutionOrder);
            Assert.AreEqual (-1, nod.Scope);
            Assert.AreEqual (1, nod.Alpha);
            Assert.AreEqual (true, nod.IsRenderingEnabled);
            Assert.AreEqual (true, nod.IsPickingEnabled);
            Assert.AreEqual (true, nod.IsCollisionEnabled);
            Assert.AreEqual (true, nod.IsLODEnabled);
            Assert.AreEqual (0, nod.ComponentCount);
        }

        [TestMethod]
        public void Test_Attach () {
            var nod = (Attachable)new Node ();
            var cmp = new Mesh ();

            nod.Attach (cmp);

            Assert.AreEqual (1, nod.ComponentCount);
            Assert.AreEqual (cmp, nod.GetComponent (0));
            Assert.AreEqual (cmp, nod.GetComponent<Mesh> ());
            Assert.AreEqual (cmp, nod.As<Mesh> ());
            Assert.AreEqual (true, nod.Is<Mesh> ());
        }

        [TestMethod]
        public void Test_Detach () {
            var nod = (Attachable)new Node ();
            var cmp = new Mesh ();

            nod.Attach (cmp);
            nod.Detach (cmp);

            Assert.AreEqual (0, nod.ComponentCount);
            Assert.AreEqual (null, nod.GetComponent<Mesh> ());
            Assert.AreEqual (null, nod.As<Mesh> ());
            Assert.AreEqual (false, nod.Is<Mesh> ());
        }


        [TestMethod]
        public void Test_AsIs () {
            var nod = (Attachable)new Node ();
            var cmp1 = new Mesh ();
            var cmp2 = new Camera ();

            nod.Attach (cmp1);
            nod.Attach (cmp2);

            Assert.AreEqual (2, nod.ComponentCount);

            Assert.AreEqual (true, nod.Is<Mesh> ());
            Assert.AreEqual (true, nod.Is<Camera> ());
            Assert.AreEqual (false, nod.Is<LODSelector> ());

            Assert.AreEqual (cmp1, nod.As<Mesh> ());
            Assert.AreEqual (cmp2, nod.As<Camera> ());
            Assert.AreEqual (null, nod.As<LODSelector> ());
        }

        [TestMethod]
        public void Test_RecvMessage () {
            var nod = (Attachable)new Node ();
            var cmp1 = new MyTestComponent ();
            var cmp2 = new MyAnotherTestComponent ();

            nod.Attach (cmp1);
            nod.Attach (cmp2);

            nod.RecvMessage ("Set1");

            Assert.AreEqual (1, cmp1.Value);
            Assert.AreEqual (1, cmp2.Value);

            nod.RecvMessage ("Set", 2);

            Assert.AreEqual (2, cmp1.Value);
            Assert.AreEqual (2, cmp2.Value);

            nod.RecvMessage<MyTestComponent> ("Set1");

            Assert.AreEqual (1, cmp1.Value);
            Assert.AreEqual (2, cmp2.Value);

            nod.RecvMessage<MyAnotherTestComponent, int> ("Set", 3);

            Assert.AreEqual (1, cmp1.Value);
            Assert.AreEqual (3, cmp2.Value);
        }

        [TestMethod]
        public void Update () {
            var nod = (Attachable)new Node ();
            var cmp1 = new MyTestComponent ();
            var cmp2 = new MyAnotherTestComponent ();

            nod.Attach (cmp1);
            nod.Attach (cmp2);

            nod.Update (0);

            Assert.AreEqual (1, cmp1.Value);
            Assert.AreEqual (1, cmp2.Value);
        }

        // RefleshとSelctのテストは保留

    }
}
